<!--
 * @Descripttion: 活动列表
 * @Author: xiao li
 * @Date: 2020-10-15 10:24:27
 * @LastEditors: xiao li
 * @LastEditTime: 2020-12-08 14:46:23
-->
<template>
    <div>
        <top-nav></top-nav>
        <div class="page-main">
          <el-row class="page-top-operate">
            <lb-button type='primary' @click="$router.push(`/app/overlord/edit`)" icon='el-icon-plus'>添加活动</lb-button>
          </el-row>
          <div class="page-search-form">
            <el-form @submit.native.prevent :inline="true" :model="searchForm" ref="searchForm">
              <el-form-item label="活动名" prop="title">
                <el-input placeholder="请输入活动名" v-model="searchForm.title"></el-input>
              </el-form-item>
              <el-form-item label="活动状态" prop="status">
                <el-select v-model="searchForm.status" placeholder="请选择">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary"  icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)" >{{$t('action.search')}}</lb-button>
                <lb-button size='medium' icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')" >{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </div>

          <el-table
              v-loading='loading'
              :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
              style="width: 100%">
                <el-table-column
                  prop="title"
                  label="活动名">
                </el-table-column>
                <el-table-column
                  label="封面图">
                  <template slot-scope="scope">
                    <lb-image :src='scope.row.cover'></lb-image>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="cate_name"
                  label="活动分类">
                </el-table-column>
                <el-table-column
                  prop="user_name"
                  label="发布人">
                </el-table-column>
                <el-table-column
                  label="活动开始时间">
                  <template slot-scope="scope">
                      <p>{{scope.row.atv_start_time | handleTime(1)}}</p>
                      <p>{{scope.row.atv_start_time | handleTime(2)}}</p>
                  </template>
                </el-table-column>
                <el-table-column
                  label="活动结束时间">
                  <template slot-scope="scope">
                      <p>{{scope.row.atv_end_time | handleTime(1)}}</p>
                      <p>{{scope.row.atv_end_time | handleTime(2)}}</p>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="num"
                  label="报名人数/店">
                </el-table-column>
                <el-table-column
                  prop="status"
                  label="活动状态">
                    <template slot-scope="scope">{{statusType[scope.row.atv_status]}}</template>
                </el-table-column>
                <el-table-column
                  prop="status"
                  label="是否启用">
                    <template slot-scope="scope">
                      <el-switch
                      v-model="scope.row.status"
                      :active-value="1"
                      :inactive-value="0"
                      @change="updateItem(scope.row.id,scope.row.status)"
                      ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                  prop="status"
                  label="操作">
                    <template slot-scope="scope">
                      <div class="table-operate">
                        <lb-button type='primary' size='mini' plain @click="$router.push(`/app/overlord/edit?id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                        <lb-button type='success' size='mini' plain @click="$router.push(`/app/overlord/signUp?id=${scope.row.id}`)">报名情况</lb-button>
                        <!-- 删除 未开始报名或活动结束后可删除 -->
                        <lb-button type='danger' size='mini' plain @click="confirmButton(scope.row)" v-if="scope.row.signup_status < 2 || scope.row.atv_status === 3">{{$t('action.delete')}}</lb-button>
                      </div>
                    </template>
                </el-table-column>
          </el-table>
          <lb-page
            :batch='false'
            :page='searchForm.page'
            :pageSize='searchForm.limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
          </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      statusOptions: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '未开始',
          value: 1
        },
        {
          label: '进行中',
          value: 2
        }, {
          label: '已结束',
          value: 3
        }
      ],
      statusType: {
        1: '未开始',
        2: '进行中',
        3: '已结束'
      },
      searchForm: {
        page: 1,
        limit: 10,
        title: '',
        status: 0
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      centerDialogVisible: false,
      dialogForm: {
        title: '',
        top: 0,
        id: ''
      },
      dialogFormRules: {
        title: {required: true, type: 'string', message: '请输入标题', trigger: 'blur'},
        top: {required: true, type: 'number', message: '请输入排序值', trigger: 'blur'}
      }
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.overlordAtvList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async  updateItem (id, status) {
      let {code} = await this.$api.overlordAtvStatusUpdate({id, status})
      if (code === 200) {
        this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
        if (status === -1) {
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        }
      } else {
        if (status === -1) return
        this.getTableDataList()
      }
    },
    confirmButton (item) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        let {id} = item
        this.updateItem(id, -1)
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select,
.el-input{
  width: 200px;
}
</style>
